<?php

namespace App\Controllers;

class Home extends BaseController {

    public function index() {
        $poet = new \App\Models\poet();
        $records = $poet->findAll();
        $table = new \CodeIgniter\View\Table();
        $headings = $poet->fields;
        $displayHeadings = array_slice($headings, 1, 3);
        $table->setHeading(array_map('ucfirst', $displayHeadings));
        foreach ($records as $record) {
            $link = anchor("writer/showme/$record->id", 'details');
            $table->addRow($record->name, '<img height="100px" src="/image/' . $record->image . '">', $link);
        }
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);
        $fields = [
            'title' => 'writer ',
            'heading' => 'Introduction to Writers',
            'footer' => 'Copyright LuGuanYu'
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

}
